<?php
namespace jy_dayfee;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
    public function jifen_sf($setting=array()){
		if($setting){
			$this->setting=$setting;
		}
        if(!$this->setting['isopen']){
            return false;
        }
        $daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $isplan = $this->pdo_get('feelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>array(3,4),'createtime >'=>$daytime),array('id'));
        if($isplan){
          return false;
        }
        $params=array('uniacid'=>$_SESSION['uniacid'],'mid >'=>0,'g_fee OR'=>'g_fee >\'0\' or a_fee >\'0\'');
        $members = pdo_getall('jy_dayfee_member',$params,array('mid','level_id','g_fee','a_fee'));
        if($members){
			$alljifen=0;
			$level=$this->plugin_setting('default_level');
            foreach($members as $item){
				if($item['level_id']){
					$level = $this->pdo_get('level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['level_id']));
				}
				$this->a_sffee($item,$level);
            }
        }
		$onejifen=count($members)>0?$alljifen/count($members):0;
        $log=array(
            'num'=>count($members),
			'onejifen'=>$onejifen,
			'alljifen'=>$alljifen,
        );
        $this->add_feelog($log,5);
        return true;
    }
	public function g_sffee($mid=0,$gnum=0){
		 //直推无需释放，按每单直推获得
		 $fromuser=pdo_getcolumn('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		 if($fromuser){
			$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			if($member['level_id']){
				$level=pdo_get('jy_dayfee_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['level_id']));
			}else{
				$level=$this->plugin_setting('default_level');
			}
		 }
		 if($gnum<=0 && !$level['g_sfrate']){
			  return false;
		 }
		 $g_sffee=$gnum*$level['g_sfrate']/100;
		 $g_sffee=$g_sffee<=$member['g_fee']?$g_sffee:$member['g_fee'];
		 if($g_sffee>0){
			pdo_update('jy_dayfee_member',array('g_fee -='=>$g_sffee,'g_sffee +='=>$g_sffee),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
		 }
		 $log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'type'=>6,
				'fee'=>$g_sffee,
				'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_dayfee_feelog',$log);
		return true;
	 }
	 public function a_sffee($member=array(),$level=array()){
		 if($member['a_fee']<=0 && !$level['a_sfrate'] && !$level['a_sfrate1'] && !$level['a_sfrate11'] && !$level['a_sfrate12'] && !$level['a_sfrate13'] && !$level['a_sfrate14']){
			  return false;
		 }
		 if($level['a_sfrate']>0){
			 $a_sffee=$member['a_fee']*$level['a_sfrate']/100;
			 $a_sffee=$a_sffee<=$member['a_fee']?$a_sffee:$member['a_fee'];
			 if($a_sffee>0){
				pdo_update('jy_dayfee_member',array('a_fee -='=>$a_sffee,'a_sffee +='=>$a_sffee),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
			 }
			 $log=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'type'=>3,
					'fee'=>$a_sffee,
					'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_dayfee_feelog',$log); 
		 }
		 if($level['a_sfrate1']>0 || $level['a_sfrate11']>0 || $level['a_sfrate12']>0 || $level['a_sfrate13']>0 || $level['a_sfrate14']>0){
			 $tuinum=pdo_count('jy_dayfee_member',array('level_id >'=>0,'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$member['mid']));
		 }
		 if($level['a_sfrate1']>0 && !$tuinum){
			 $members=pdo_getall('jy_dayfee_member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$member['mid']));
			 $mlevel=$this->plugin_setting('default_level');
			 $alla_sffee=0;
            foreach($members as $item){
				if($item['level_id']){
					$mlevel = $this->pdo_get('level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['level_id']));
				}
				$a_sffee=$item['a_fee']*$mlevel['a_sfrate1']/100;
				$a_sffee=$a_sffee<=$item['a_fee']?$a_sffee:$item['a_fee'];
				$alla_sffee +=$a_sffee;
            }
			$mya_sffee=$alla_sffee*$level['a_sfrate']/100;
			$mya_sffee=$mya_sffee<=$member['a_fee']?$mya_sffee:$member['a_fee'];
			 if($mya_sffee>0){
				pdo_update('jy_dayfee_member',array('a_fee -='=>$mya_sffee,'a_sffee +='=>$mya_sffee),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
			 }
			 $log=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'type'=>4,
					'fee'=>$mya_sffee,
					'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_dayfee_feelog',$log);
		 }
		 if($tuinum>0){
			$members=pdo_getall('jy_dayfee_member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$member['mid']));
			 $mlevel=$this->plugin_setting('default_level');
			 $alla_sffee=0;
			 if($tuinum >=1){
				$a_sfrate1= $level['a_sfrate11'];
			 }
			 if($tuinum >=2){
				$a_sfrate1= $level['a_sfrate12'];
			 }
			 if($tuinum >=3){
				$a_sfrate1= $level['a_sfrate13'];
			 }
			 if($tuinum >=4){
				$a_sfrate1= $level['a_sfrate14'];
			 }
            foreach($members as $item){
				$a_sffee=$item['a_fee']*$a_sfrate1/100;
				$a_sffee=$a_sffee<=$item['a_fee']?$a_sffee:$item['a_fee'];
				$alla_sffee +=$a_sffee;
            }
			$mya_sffee=$alla_sffee*$level['a_sfrate']/100;
			$mya_sffee=$mya_sffee<=$member['a_fee']?$mya_sffee:$member['a_fee'];
			 if($mya_sffee>0){
				pdo_update('jy_dayfee_member',array('a_fee -='=>$mya_sffee,'a_sffee +='=>$mya_sffee),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
			 }
			 $log=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'type'=>4,
					'fee'=>$mya_sffee,
					'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_dayfee_feelog',$log); 
		 }
		 return true;
	 }
}